import { Op } from "sequelize";
import dictModel from "../model/dict.model"

class DictService {
    //创建字典类型
    async createType({name, code}){
        let res = await dictModel.DictType.create({name, code});
        return res.dataValues;
    }
    //查重
    async findTypeInfo({name,code}){
        if(name){
            let res = await dictModel.DictType.findOne({
                attributes: ['id', 'name', 'code'],
                where: {name}
            });
            if(res?.dataValues){
                return res?.dataValues
            }
        }
        if(code){
            let res = await dictModel.DictType.findOne({
                attributes: ['id', 'name', 'code'],
                where: {code}
            });
            return res?.dataValues || null
        }
    }
    //删除类型
    async deleteType(ids){
        const res = await dictModel.DictType.destroy({ where: { id:ids } })
        return res > 0 ? true : false;
    }
    //根据id更新type
    async updateTypeById({id, name, code}){
        let whereOpt = { id };
        let params = {};
        name && Object.assign(params, { name })
        code && Object.assign(params, { code })
        const res = await dictModel.DictType.update(params, { where: whereOpt });
        return res[0] > 0 ? true : false
    }
    //类型列表
    async getTypeList({id='', name='', code='', size=10, page=1 }){
        let whereOpt = {};
        name && Object.assign(whereOpt, { name:{[Op.like]: `%${name}%`}})
        id && Object.assign(whereOpt, { id })
        code && Object.assign(whereOpt, { code })
        let res:any = null
        res = await dictModel.DictType.findAndCountAll({
            where:whereOpt,
            offset: (page - 1) * size,
            limit: size,
            raw: true,
        })
        return res || null
    }

    //查值
    async findDataInfo({dict_label,dict_value,dict_code}){
        let whereOpt = {dict_code}
        if(dict_label){
            let options = {...whereOpt,dict_label}
            let res = await dictModel.DictData.findOne({
                attributes: ['id', 'dict_code', 'dict_label','dict_value'],
                where: options
            });
            if(res?.dataValues){
                return res?.dataValues
            }
        }
        if(dict_value){
            let options = {...whereOpt,dict_value}
            console.log(options)
            let res = await dictModel.DictData.findOne({
                attributes: ['id', 'dict_code', 'dict_label','dict_value'],
                where: options
            });
            return res?.dataValues || null
        }
    }

    async createData({dict_label,dict_code,dict_value,dict_color}){
        let res = await dictModel.DictData.create({dict_label,dict_code,dict_value,dict_color});
        return res.dataValues;
    }
    async updateDataById({id,dict_label,dict_code,dict_value,dict_color}){
        let whereOpt = { id };
        let params = {};
        dict_label && Object.assign(params, { dict_label })
        dict_code && Object.assign(params, { dict_code })
        dict_value && Object.assign(params, { dict_value })
        dict_color && Object.assign(params, { dict_color })
        const res = await dictModel.DictData.update(params, { where: whereOpt });
        return res[0] > 0 ? true : false
    }
    //删除值
    async deleteData(id){
        const res = await dictModel.DictData.destroy({ where: { id } })
        return res > 0 ? true : false;
    }
    //值列表
    async getDataList({id, dict_label , dict_code ,dict_value,dict_color, size=10, page=1 }){
        let whereOpt = {dict_code};
        dict_label && Object.assign(whereOpt, { dict_label:{[Op.like]: `%${dict_label}%`}})
        id && Object.assign(whereOpt, { id })
        dict_value && Object.assign(whereOpt, { dict_value })
        dict_color && Object.assign(whereOpt, { dict_color })
        let res:any = null
        res = await dictModel.DictData.findAndCountAll({
            where:whereOpt,
            offset: (page - 1) * size,
            limit: size,
            raw: true,
        })
        return res || null
    }
}

export default new DictService